package com.example.controller;


import com.example.common.annotation.OperLog;
import com.example.common.annotation.RepeatSubmit;
import com.example.common.enums.LogType;
import com.example.common.utils.R;
import com.example.system.entity.Notice;
import com.example.system.service.INoticeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王鹤蒲
 * @since 2023-09-04
 */
@Api(tags = "公告信息API",description="提供公共信息相关的 REST API")
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService iNoticeService;

    @OperLog(operation = "通知公告",type = LogType.ADD)
    @RepeatSubmit(lockTime = 5)
    @RequestMapping(value="/saveNotice",method= RequestMethod.POST)
    @ResponseBody
    public R saveNotice(@RequestBody Notice notice) {
        return iNoticeService.saveNotice(notice);
    }

    @OperLog(operation = "通知公告",type = LogType.UPDATE)
    @RepeatSubmit(lockTime = 5)
    @RequestMapping(value="/edit",method= RequestMethod.POST)
    @ResponseBody
    public R updateNotice(@RequestBody Notice notice) {
        return iNoticeService.updateNotice(notice);
    }

    @OperLog(operation = "通知公告",type = LogType.DELETE)
    @RequestMapping(value="/delete/{id}",method= RequestMethod.DELETE)
    @ResponseBody
    public R delete(@PathVariable("id") Integer id) {
        return iNoticeService.delete(id);
    }

    @OperLog(operation = "通知公告",type = LogType.BATCH_DELETE)
    @RequestMapping(value="/deleteBatch",method= RequestMethod.POST)
    @ResponseBody
    public R deleteBatch(@RequestBody Integer[] ids) {
        return iNoticeService.deleteBatch(ids);
    }

    @RequestMapping(value="/{id}",method= RequestMethod.GET)
    @ResponseBody
    public R findOne(@PathVariable("id")Integer id) {
        return iNoticeService.findOne(id);
    }

    @RequestMapping(value="/findAll",method= RequestMethod.GET)
    @ResponseBody
    public R findAll() {
        return iNoticeService.findAll();
    }

    @RequestMapping(value="/type/{type}",method= RequestMethod.GET)
    @ResponseBody
    public R findAllByType(@PathVariable("type") Integer type) {
        return iNoticeService.findAllByType(type);
    }

    @RequestMapping(value="/page",method= RequestMethod.GET)
    @ResponseBody
    public R findPage(@RequestParam(defaultValue = "")String title, @RequestParam int currentPage, @RequestParam int pageSize) {
        return iNoticeService.findPage(title,currentPage, pageSize);
    }

    @RequestMapping(value="/import",method= RequestMethod.POST)
    @RepeatSubmit(lockTime = 5)
    @ResponseBody
    public R importNotice(MultipartFile file) {
        return iNoticeService.importNotice(file);
    }

    @RequestMapping(value="/export",method= RequestMethod.GET)
    @RepeatSubmit(lockTime = 5)
    @ResponseBody
    public void export(HttpServletResponse httpServletResponse) {
        iNoticeService.export(httpServletResponse);
    }


}



